package com.go.where.family.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.TopicComponent;
import com.go.where.family.constants.Constant;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.TopicInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.TopicService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.TopicVo;
import com.google.gson.Gson;

@Component("topicComponent")
public class TopicComponentImpl implements TopicComponent {

	private static final Logger log = Logger
			.getLogger(TopicComponentImpl.class);
	private Gson gson;
	@Resource(name = "topicService")
	private TopicService topicService;

	public TopicComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<TopicInfo> queryList(String body, Head head) {
		TopicVo vo = gson.fromJson(body, TopicVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageStart", pageStart);
			map.put("pageEnd", pageEnd);
			map.put("isDelete", Constant.IS_NOT_DELETE);
			map.put("month", vo.getMonth());
			if (StringUtils.isNotBlank(vo.getOrderBy())) {
				if (Constant.ORDER_TYPE_ASC.equals(vo.getOrderType())) {
					map.put("page_orderByAsc", vo.getOrderBy());
				} else {
					map.put("page_orderByDesc", vo.getOrderBy());
				}
			}

			List<TopicInfo> list = topicService.queryList(map);
			if (list != null && !list.isEmpty()) {
				for (TopicInfo topic : list) {
					if (StringUtils.isNotBlank(topic.getPrePhotoPath())) {
						topic.setPrePhotoPath(CommonUtil.getUrlPrefix()
								+ topic.getPrePhotoPath());
					}
					if (StringUtils.isNotBlank(topic.getMainPhotoPath())) {
						topic.setMainPhotoPath(CommonUtil.getUrlPrefix()
								+ topic.getMainPhotoPath());
					}
				}
				return list;
			} else {
				return new ArrayList<TopicInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取课题列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public TopicInfo queryTopicDetail(String body, Head head) {
		TopicVo vo = gson.fromJson(body, TopicVo.class);
		if (vo != null) {
			String topicId = vo.getTopicId();
			TopicInfo vv = topicService.queryTopicDetail(topicId);
			if (vv != null) {
				if (StringUtils.isNotBlank(vv.getPrePhotoPath())) {
					vv.setPrePhotoPath(CommonUtil.getUrlPrefix()
							+ vv.getPrePhotoPath());
				}
				if (StringUtils.isNotBlank(vv.getMainPhotoPath())) {
					vv.setMainPhotoPath(CommonUtil.getUrlPrefix()
							+ vv.getMainPhotoPath());
				}
				return vv;
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",查询课题详情信息时,没有查询到数据,课题ID：" + topicId);
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取课题详情信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public boolean updateLike(String body, Head head) {
		TopicVo vo = gson.fromJson(body, TopicVo.class);
		if (vo != null) {
			String tid = vo.getTopicId();
			TopicInfo ti = topicService.queryTopicDetail(tid);
			if (ti != null) {
				Integer likeNum = ti.getLikeNum();
				if ("1".equals(vo.getLikeOrNot())) {
					likeNum = CommonUtil.add(likeNum, 1);
				} else if ("0".equals(vo.getLikeOrNot())) {
					likeNum = CommonUtil.sub(likeNum, 1);
				}
				TopicInfo topic = new TopicInfo();
				topic.setTopicId(tid);
				topic.setLikeNum(likeNum);
				int re = topicService.update(topic);
				if (re > 0) {
					return true;
				} else {
					return false;
				}
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",更新课题点赞量时,没有查询到数据,课题ID：" + tid);
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",更新课题点赞量，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}
}
